package com.bs.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bs.entity.Admin;
import com.bs.entity.AdminInfo;
import com.bs.entity.Employee;
import com.bs.mapper.AdminInfoMapper;
import com.bs.service.AdminInfoService;
import com.bs.service.AdminService;
import com.bs.service.ClockInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

@RestController
@RequestMapping("/AdminInfo")
public class AdminInfoCon {
    @Autowired
    AdminInfoService adminInfoService;

    @Autowired
    AdminService adminService;

    @Autowired
    ClockInfoService clockInfoService;


    @GetMapping("/selectById")
    public AdminInfo selectById(@RequestParam Integer id){
       return adminInfoService.getById(id);
    }

    @GetMapping("/selectALL")
    public List<AdminInfo> selectAll(){
        List<AdminInfo> list = adminInfoService.list();
        return list;
    }


    //    新增和修改管理员
    @PostMapping("/saveAdmin")
    public Boolean saveAdmin(AdminInfo adminInfo){
        return adminInfoService.saveAdmin(adminInfo);
    }

    @DeleteMapping("/delete/{id}")
    public Boolean deleteAdminInfo(@PathVariable Integer id){
        return adminInfoService.removeById(id);
    }

    @GetMapping("/login")
    public Boolean login(@RequestParam Integer id,
                         @RequestParam String password){
        QueryWrapper<Admin> queryWrapper = new QueryWrapper<Admin>();
        queryWrapper.lambda().eq(Admin::getId,id).eq(Admin::getPassword,password);
        Admin one = adminService.getOne(queryWrapper);
        if (Objects.isNull(one)){
            return false;
        }else {
            return true;
        }
    }
    /**
     * 分页查询
     * @param pageNum
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public IPage<AdminInfo> findPage(@RequestParam Integer pageNum
            , @RequestParam Integer pageSize
            , @RequestParam(required = false) String name
            , @RequestParam(required = false) Integer id){
        IPage<AdminInfo> page = new Page<>(pageNum,pageSize);
        QueryWrapper<AdminInfo> queryWrapper = new QueryWrapper<>();
        if (Objects.nonNull(name)){
            queryWrapper.lambda().like(AdminInfo::getName,name);
        }
        if(Objects.nonNull(id)){
            queryWrapper.lambda().eq(AdminInfo::getId,id);
        }
        return adminInfoService.page(page, queryWrapper);
    }


    @PostMapping("/changePwd")
    public Boolean changePwd(@RequestBody Map<String,String> data){
        Integer passwordFlag = clockInfoService.getPwd();
        if(passwordFlag == 0){
            return false;
        }
        Integer id = Integer.valueOf(data.get("id"));
        String password = data.get("password");
        String rePassword = data.get("repassword");
        return adminService.updatePwd(id,password,rePassword);
    }
}
